package com.example.demo.service;

import com.example.demo.exceptions.DatabaseOperationException;
import com.example.demo.exceptions.RecordExistsException;
import com.example.demo.exceptions.RecordNotFoundException;
import com.example.demo.model.CheckRole;
import com.example.demo.model.PersonalInfo;
import com.example.demo.repository.PersonalInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class PersonalInfoService {

    @Autowired
    private PersonalInfoRepository personalInfoRepository;

//    @CheckRole("ROLE_ADMIN")
    public PersonalInfo createPersonalInfo(PersonalInfo personalInfo) {
        // 检查是否存在相同的记录 （根据名字和电话号码）
        if (personalInfoRepository.existsByNameAndPhoneNumber(personalInfo.getName(), personalInfo.getPhoneNumber())) {
            throw new RecordExistsException(null);
        }
        return personalInfoRepository.save(personalInfo);
    }

//    @CheckRole("ROLE_USER")
    public PersonalInfo updatePersonalInfo(int id, PersonalInfo updatedInfo) {
        // 获取现有记录
        Optional<PersonalInfo> optionalInfo = personalInfoRepository.findById(id);

        if (optionalInfo.isPresent()) {
            PersonalInfo existingInfo = optionalInfo.get();

            // 更新字段
            if (updatedInfo.getName() != null) {
                existingInfo.setName(updatedInfo.getName());
            }
            if (updatedInfo.getAge() != null) {
                existingInfo.setAge(updatedInfo.getAge());
            }
            if (updatedInfo.getGender() != null) {
                existingInfo.setGender(updatedInfo.getGender());
            }
            if (updatedInfo.getEmail() != null) {
                existingInfo.setEmail(updatedInfo.getEmail());
            }
            if (updatedInfo.getPhoneNumber() != null) {
                existingInfo.setPhoneNumber(updatedInfo.getPhoneNumber());
            }
            if (updatedInfo.getAddress() != null) {
                existingInfo.setAddress(updatedInfo.getAddress());
            }
            if (updatedInfo.getPassword() != null && ! updatedInfo.getPassword().trim().isEmpty()) {
                existingInfo.setPassword(updatedInfo.getPassword());
            }

            // 保存更新后的记录
            return personalInfoRepository.save(existingInfo);
        } else {
            throw new RecordNotFoundException(null);
        }
    }


//    @CheckRole("ROLE_ADMIN")
    public void deletePersonalInfo(Integer id) {
        // 验证记录是否存在
        if (!personalInfoRepository.existsById(id)) {
            throw new RecordNotFoundException(null);
        }
        try {
            personalInfoRepository.deleteById(id);
        } catch (Exception e) {
            throw new DatabaseOperationException(null);
        }
    }

//    @CheckRole("ROLE_USER")
    public Optional<PersonalInfo> getPersonalInfoById(Integer id) {
        return personalInfoRepository.findById(id);
    }

//    @CheckRole("ROLE_USER")
    public List<PersonalInfo> getAllPersonalInfo() {
        return personalInfoRepository.findAll();
    }

    // 新增一个方法来检查是否存在具有相同名字和电话号码的记录
    public boolean checkIfPersonalInfoExists(String name, String phoneNumber) {
        return personalInfoRepository.existsByNameAndPhoneNumber(name, phoneNumber);
    }

//    @Autowired
//    private ConversionService conversionService;
//
//    // 处理电话号码
//    public void maskPhoneNumber(PersonalInfo personalInfo) {
//        // 使用转换器对电话号码进行脱敏
//        personalInfo.setPhoneNumber(conversionService.convert(personalInfo.getPhoneNumber(), String.class));
//    }
//
//    // 处理多个对象的电话号码
//    public void maskPhoneNumbers(List<PersonalInfo> personalInfos) {
//        personalInfos.forEach(this::maskPhoneNumber);
//    }
//
//    // 处理密码
//    public void replacePassword(PersonalInfo personalInfo) {
//        personalInfo.setPassword("******"); // 替换为“******”形式
//    }
//
//    // 处理多个对象的密码
//    public void replacePasswords(List<PersonalInfo> personalInfos) {
//        personalInfos.forEach(this::replacePassword);
//    }

}